37 #include "sensor_drv.h" 38 #include "sensor_io_i2c.h" 41 #define MMA8652_COUNTSPERG 8192.0 42 #define MMA8652_ACCEL_FIFO_SIZE 32 47 const registerreadlist_t MMA8652_WHO_AM_I_READ[] =
49 { .readFrom = MMA865x_WHO_AM_I, .numBytes = 1 }, __END_READ_DATA__
53 const registerreadlist_t MMA8652_F_STATUS_READ[] =
55 { .readFrom = MMA865x_STATUS, .numBytes = 1 }, __END_READ_DATA__
59 registerreadlist_t MMA8652_DATA_READ[] =
61 { .readFrom = MMA865x_OUT_X_MSB, .numBytes = 6 }, __END_READ_DATA__
65 const registerwritelist_t MMA8652_Initialization[] =
70 { MMA865x_CTRL_REG1, 0x00, 0x00 },
75 { MMA865x_F_SETUP, 0x40, 0x00 },
82 { MMA865x_XYZ_DATA_CFG, 0x01, 0x00 },
91 { MMA865x_CTRL_REG2, 0x02, 0x00 },
106 #if (ACCEL_ODR_HZ <= 1) 107 { MMA865x_CTRL_REG1, 0x39, 0x00 },
108 #elif (ACCEL_ODR_HZ <= 6) 109 { MMA865x_CTRL_REG1, 0x31, 0x00 },
110 #elif (ACCEL_ODR_HZ <= 12) 111 { MMA865x_CTRL_REG1, 0x29, 0x00 },
112 #elif (ACCEL_ODR_HZ <= 50) 113 { MMA865x_CTRL_REG1, 0x21, 0x00 },
114 #elif (ACCEL_ODR_HZ <= 100) 115 { MMA865x_CTRL_REG1, 0x19, 0x00 },
116 #elif (ACCEL_ODR_HZ <= 200) 117 { MMA865x_CTRL_REG1, 0x11, 0x00 },
118 #elif (ACCEL_ODR_HZ <= 400) 119 { MMA865x_CTRL_REG1, 0x09, 0x00 },
121 { MMA865x_CTRL_REG1, 0x01, 0x00 },
135 status = Register_I2C_Read(sensor->
bus_driver, sensor->
addr, MMA865x_WHO_AM_I, 1, ®);
136 if (status==SENSOR_ERROR_NONE) {
137 sfg->Accel.iWhoAmI = reg;
138 if (reg != MMA8652_WHOAMI_VALUE)
return(SENSOR_ERROR_INIT);
143 status = Sensor_I2C_Write(sensor->
bus_driver, sensor->
addr, MMA8652_Initialization );
150 sfg->Accel.isEnabled =
true;
160 uint8_t sensor_fifo_count = 1;
165 return SENSOR_ERROR_INIT;
169 status = Sensor_I2C_Read(sensor->
bus_driver, sensor->
addr, MMA8652_F_STATUS_READ, I2C_Buffer );
170 if (status==SENSOR_ERROR_NONE) {
171 sensor_fifo_count = I2C_Buffer[0] & 0x3F;
174 if (sensor_fifo_count == 0)
return SENSOR_ERROR_READ;
179 MMA8652_DATA_READ[0].readFrom = MMA865x_OUT_X_MSB;
180 MMA8652_DATA_READ[0].numBytes = 6 * sensor_fifo_count;
181 status = Sensor_I2C_Read(sensor->
bus_driver, sensor->
addr, MMA8652_DATA_READ, I2C_Buffer );
183 if (status==SENSOR_ERROR_NONE) {
184 for (j = 0; j < sensor_fifo_count; j++)
186 sample[
CHX] = (I2C_Buffer[6 * j ] << 8) | I2C_Buffer[6 * j + 1];
187 sample[
CHY] = (I2C_Buffer[6 * j + 2] << 8) | I2C_Buffer[6 * j + 3];
188 sample[
CHZ] = (I2C_Buffer[6 * j + 4] << 8) | I2C_Buffer[6 * j + 5];
199 const registerwritelist_t MMA8652_IDLE[] =
202 { MMA865x_CTRL_REG1, 0x00, 0x01 },
211 status = Sensor_I2C_Write(sensor->
bus_driver, sensor->
addr, MMA8652_IDLE );
213 sfg->Accel.isEnabled =
false;
215 return SENSOR_ERROR_INIT;
220 #endif // F_USING_ACCEL #define MMA8652_ACCEL_FIFO_SIZE
#define CHY
Used to access Y-channel entries in various data data structures.
void addToFifo(FifoSensor *sensor, uint16_t maxFifoSize, int16_t sample[3])
addToFifo is called from within sensor driver read functions
The top level fusion structure.
void * bus_driver
should be of type (ARM_DRIVER_I2C* for I2C-based sensors, ARM_DRIVER_SPI* for SPI) ...
#define ACCEL_FIFO_SIZE
FXOS8700 (accel), MMA8652, FXLS8952 all have 32 element FIFO.
The FifoSensor union allows us to use common pointers for Accel, Mag & Gyro logical sensor structures...
The sensor_fusion.h file implements the top level programming interface.
Provides function prototypes for driver level interfaces.
int8_t MMA8652_Read(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define CHX
Used to access X-channel entries in various data data structures.
int8_t MMA8652_Init(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
void conditionSample(int16_t sample[3])
conditionSample ensures that we never encounter the maximum negative two's complement value for a 16-...
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
uint16_t isInitialized
Bitfields to indicate sensor is active (use SensorBitFields from build.h)
int8_t MMA8652_Idle(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define F_USING_ACCEL
nominally 0x0001 if an accelerometer is to be used, 0x0000 otherwise
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.
uint16_t addr
I2C address if applicable.
#define MMA8652_COUNTSPERG